package com.v5.ik;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zy on 2016/9/26.
 */
public class PYQAdapter extends BaseAdapter {
    private FragmentActivity activity;
    private List<PYQSource> pyqSources;
    final LikeWechatDialog likeWechatDialog;

    public PYQAdapter(FragmentActivity activity,List<PYQSource> pyqSources) {
        this.activity = activity;
        likeWechatDialog = new LikeWechatDialog(activity, "请稍等...");
        this.pyqSources = pyqSources;
    }

    @Override
    public int getCount() {
        return pyqSources == null ? 0 : pyqSources.size();
    }

    @Override
    public PYQSource getItem(int i) {
        return pyqSources.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = View.inflate(viewGroup.getContext(), R.layout.pyq_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final PYQSource pyqSource = getItem(i);
        viewHolder.tv_name.setText(pyqSource.getName());
        ImgGridAdapter imgGridAdapter = new ImgGridAdapter(activity,pyqSource.getPicArr().split(","));
        viewHolder.gv_img.setAdapter(imgGridAdapter);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(new Date(((long)Integer.valueOf(pyqSource.getTime()))*1000));
        viewHolder.tv_time.setText(dateString);
        viewHolder.tv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                likeWechatDialog.show();
                new AsyncTask<String, Void, List<Bitmap>>() {
                    @Override
                    protected List<Bitmap> doInBackground(String... params) {
                        List<Bitmap> bitmapList = new ArrayList<>();
                        for (String img:pyqSource.getPicArr().split(",")) {
                            bitmapList.add(Utils.downloadImg(img));
                        }
                        return bitmapList;
                    }

                    @Override
                    protected void onPostExecute(List<Bitmap> bitmapList) {
                        if (bitmapList == null || bitmapList.size() == 0)
                            return;

                        Intent shareIntent = new Intent();
                        ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
                        shareIntent.setComponent(componentName);
                        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
                        shareIntent.putExtra("Kdescription", pyqSource.getName());
                        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);

                        ArrayList<Uri> uris = new ArrayList<>();
                        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ik");
                        if (!file.isDirectory()) {
                            file.mkdirs();
                        }
                        try {
                            for (int i = 0; i < bitmapList.size(); i++) {
                                File outFile = new File(file.getPath() + "/" + System.currentTimeMillis() + ".jpg");
                                OutputStream os = new FileOutputStream(outFile);
                                Bitmap bitmap = bitmapList.get(i);
                                if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os)) {
                                    uris.add(Uri.fromFile(outFile));
                                }
                                os.close();
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                        shareIntent.setType("image/*");
                        activity.startActivity(shareIntent);
                        likeWechatDialog.dismiss();
                    }
                }.execute();
            }
        });

        return view;
    }

    class ViewHolder {
        TextView tv_name,tv_time,tv_share;
        ImgGridView gv_img;

        public ViewHolder(View view) {
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            gv_img = (ImgGridView) view.findViewById(R.id.gv_img);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_share = (TextView) view.findViewById(R.id.tv_share);
        }
    }
}
